//
//  TCSdColloctViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/4.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class TCSdColloctViewController: NSViewController {
    
    var identifier_ktitle =  NSToolbarItem.Identifier.init(rawValue: "identifier_ktitle")
    var info:TRTCScreenCaptureSourceInfo?
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, 720, 500))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.hk_refactoringWindow()
    }
    func hk_refactoringWindow() {
        self.title = ""
        self.view.window?.titlebarAppearsTransparent = true
        self.view.window?.titleVisibility = .hidden
        self.view.window?.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)?.isHidden = true
        self.view.window?.standardWindowButton(NSWindow.ButtonType.zoomButton)?.isHidden = true
        self.view.window?.toolbar = self.toolbar
        
    }
    
    lazy var ktitle: NSLabel = {
        let lab = NSLabel.label(title: "共享", textColor: color_gray(0), font: NSFont.systemFont(ofSize: 16))
        lab.alignment = .center
        lab.sizeToFit()
        return lab
    }()
    
    lazy var toolbar: NSToolbar = {
        let tool = NSToolbar.init(identifier: "TCSdColloctViewController")
        tool.delegate = self
        return tool
    }()
    
    func setupUI() {
        self.view.addSubview(ckCollect)
        ckCollect.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(kNavBarHeight-kNavDefBarHeight, 20, 70, 20))
        }
        
        self.view.addSubview(confirmBtn)
        confirmBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(-30)
            make.centerX.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(32)
        }
        
    }
    lazy var confirmBtn: ZHButton = {
        let btn = ZHButton.init()
        btn.zh_title = "开启共享"
        btn.zh_font = NSFont.systemFont(ofSize: 14)
        btn.zh_color = NSColor.white
        btn.hasBorder = true
        btn.zh_cornerRadius = 16
        btn.zh_backgroundColor = color_main140
        btn.action = #selector(confirmBtnAction(senber:))
        btn.target = self
        return btn
    }()
    
    @objc func openScreen() {
        let source = self.info
        if source != nil {
            TrtcEngine.enterScreenShare(info: source!)
        }else {
            TXLiteManager.shared().stopScreenCapture()
        }
    }
    
    
    @objc func confirmBtnAction(senber:ZHButton) {
        if Td.sh().sd.isSding == true {
            let rect = self.info?.type == TRTCScreenCaptureSourceType.screen ? NSScreen.main!.frame : CGRect.zero
            let highlight = !(self.info?.type == TRTCScreenCaptureSourceType.screen)
            TRTCCloud.sharedInstance()?.selectScreenCaptureTarget(self.info, rect: rect, capturesCursor: true, highlight: highlight )
            TrtcEngine.close(classs: [TCSdColloctViewController.classForCoder()])
            //NotificationCenter.default.post(name: MTSendStreamNotification, object: StreamMode.init(userId: Td.sh().params.userId!, available: false))
            //self.perform(#selector(openScreen), with: nil, afterDelay: 0.2)
        }else {
            self.openScreen()
        }
        
    }
    
    lazy var ckCollect: CKCollectionView = {
        let view = CKCollectionView.init(frame: NSRect.zero)
        view.delegate = self
        return view
    }()

  
}

extension TCSdColloctViewController:CKCollectionDelegate {
    func ck_collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>, info: TRTCScreenCaptureSourceInfo) {
        self.info = info
    }
}


extension TCSdColloctViewController:NSToolbarDelegate {
    func toolbarIdentifiers() -> [NSToolbarItem.Identifier] {
        return [
            NSToolbarItem.Identifier.flexibleSpace,
            identifier_ktitle,
            NSToolbarItem.Identifier.flexibleSpace,
        ]
    }
    
    func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return self.toolbarIdentifiers()
    }
    
    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return self.toolbarIdentifiers()
        
    }
    
    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        let toolbarItem = NSToolbarItem.init(itemIdentifier: itemIdentifier)
        
        if itemIdentifier.rawValue.isEqual(identifier_ktitle.rawValue) {
            toolbarItem.view = self.ktitle
            return toolbarItem
        }else {
            return nil
        }
    }
}
